import React, {Component} from 'react';

import './index.less';

import getEditTemplate from './request/getEditTreeTemplate';
import deleteTreeNode from './request/deleteGroup';
import enabledGroup from './request/enabledGroup';

import {
    promptBox,
    toast
} from 'nc-lightapp-front';

import language from '../../language';

class LeftTree extends Component {
    constructor(props) {
        super(props);

        this.openEditTreeNodeModal = this.openEditTreeNodeModal.bind(this);
        this.deleteTreeNode = this.deleteTreeNode.bind(this);
        this.buttonClickHandle = this.buttonClickHandle.bind(this);
        this.enabledGroup = this.enabledGroup.bind(this);
    }

    buttonClickHandle(props, btnCode) {

        // 新增组
        if(btnCode === 'grpadd') {
            this.openEditTreeNodeModal('add')();
        }
        // 修改组
        else if(btnCode === 'grpedit') {
            this.openEditTreeNodeModal('change')();
        }
        // 删除组
        else if(btnCode === 'grpdel') {
            this.deleteTreeNode();
        }
        // 刷新组
        else if(btnCode === 'grpref') {
            this.props.getLeftTreeData();
        }
        // 启用组
        else if(btnCode === 'grpena') {
            this.enabledGroup(true);
        }
        else if(btnCode === 'grpdis') {
            this.enabledGroup(false);
        }

        this.props.doDisabledTreeNodeList();
    }

    enabledGroup(type) {
        const {
            currentSelectDepartment,
            humanResourceOrganizationValue,
            syncTree,
            doDisabledTreeNodeList,
            button,
            editTable
        } = this.props;

        let treeData = syncTree.getSyncTreeValue('keyPsnGroupTree', currentSelectDepartment);
        let postData = {
            pk_keypsn_group: treeData.nodeData.nodeValue['pk_keypsn_group'],
            pk_org: humanResourceOrganizationValue.refpk
        };

        promptBox({
            color: 'warning',
            title: type ? language['hi6007-000222'] : language['hi6007-000220'], // 确认停用
            content: type ? language['hi6007-000223'] : language['hi6007-000221'], // 是否确认要停用,
            beSureBtnClick: () => {
                enabledGroup(postData, type)
                    .then((res) => {
                        let treeData = syncTree.getSyncTreeValue('keyPsnGroupTree');
                        treeData[0].children.map((item) => {
                            if(item.refpk === res.data[0].refpk) {
                                res.data[0].children = item.children;
                                Object.assign(item, res.data[0]);
                                this.forceUpdate();
                            }
                        });
                        doDisabledTreeNodeList(treeData);

                        button.setButtonDisabled({
                            grpena: type,
                            grpdis: !type,
                            refadd: !type
                        });
                        toast({
                            color: 'success',
                            content: type ? language['hi6007-000218'] : language['hi6007-000219'], // 启用成功, 停用成功
                        });
                        // editTable.setAllCheckboxAble('ncc60071025list', type);
                    });
            }
        });

        
    }

    deleteTreeNode() {
        const {
            currentSelectDepartment,
            humanResourceOrganizationValue,
            syncTree
        } = this.props;
        promptBox({
            color: 'warning',
            title: language['hi6007-000200'], //'确认删除',
            content: language['hi6007-000007'], // '您确定要删除所选数据吗',
            beSureBtnClick: () => {
                let treeData = syncTree.getSyncTreeValue('keyPsnGroupTree', currentSelectDepartment);
                let postData = {
                    pk_keypsn_group: treeData.nodeData.nodeValue['pk_keypsn_group'],
                    pk_org: humanResourceOrganizationValue.refpk
                };
                deleteTreeNode(postData)
                    .then((res) => {
                        toast({
                            color: 'success',
                            content: language['hi6007-000006'] // 删除成功
                        });
                        syncTree.delNodeSuceess('keyPsnGroupTree', currentSelectDepartment);
                    });
            }
        });
    }

    openEditTreeNodeModal(type) {
        return () => {
            const {
                humanResourceOrganizationValue,
                currentSelectDepartment,
                updateState,
                syncTree
            } = this.props;

            let postData = {
                pk_org: humanResourceOrganizationValue.refpk
            };

            let treeData = syncTree.getSyncTreeValue('keyPsnGroupTree', currentSelectDepartment);

            if(type === 'change') {
                postData['pk_keypsn_group'] = treeData.nodeData.nodeValue['pk_keypsn_group'];
            }
            getEditTemplate(type, postData)
                .then((res) => {
                    updateState({
                        editTreeNodeModalVisible: true,
                        currentEditTreeNodeData: res.data['keypsngrp'],
                        currentEditNodeType: type
                    });
                });
        }
    }

    render() {
        const {syncTree, selectTree, button} = this.props;
        
        return (
            <div id="leftTreeList" className="left-tree-list-wrapper">
                <div className="left-tree-operator">
                    {button.createButtonApp({
                        area: 'tree',
                        onButtonClick: this.buttonClickHandle
                    })}
                </div>
                {syncTree.createSyncTree({
                    treeId: 'keyPsnGroupTree',
                    needSearch: false,
                    needEdit: false,
                    checkable: false,
                    defaultExpandAll: false,
                    showModal: false,
                    onSelectEve: selectTree,
                    clickAddIconEve: this.openEditTreeNodeModal('add'),
                    clickEditIconEve: this.openEditTreeNodeModal('change'),
                    clickDelIconEve: this.deleteTreeNode
                })}
            </div>
        );
    }
}


export default LeftTree;